//
//  ModifyPwdViewController.m
//  NOAHWM
//
//  Created by Ryan on 13-6-3.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "ModifyPwdViewController.h"
#import "NYMemberCenterDataManager.h"
#import "NSString+MD5.h"
#import "JSON.h"
#import "ModifyPwdViewController.h"
#import "ForgetViewController.h"

@interface ModifyPwdViewController ()

@end

@implementation ModifyPwdViewController

@synthesize oldPwdField,pwdField,repwdField,forgetPwdButton;
@synthesize myScrollView;

- (IBAction)forgetPwdButtonClicked:(id)sender
{
    ForgetViewController *fvc = [[ForgetViewController alloc] init];
    [self.navigationController pushViewController:fvc animated:YES];
    [fvc release];
}

- (IBAction)saveButtonPressed:(id)sender {
    [self.oldPwdField resignFirstResponder];
    [self.pwdField resignFirstResponder];
    [self.repwdField resignFirstResponder];
    
    NSString *validString = [self checkValid];
    if(validString)
    {
        [[RYHUDManager sharedManager] showWithMessage:validString customView:nil hideDelay:2.f];
    }
    else
    {
        NSString *md5OldPwd = [NSString md5HexDigest:oldPwdField.text];
        NSString *md5Pwd = [NSString md5HexDigest:pwdField.text];
        NSString *md5Repwd = [NSString md5HexDigest:repwdField.text];
        
        NSString *suburl = [NSString stringWithFormat:kModifyPwdUrl,@"jsonp",[NYMemberCenterDataManager sharedManager].loginMember.accountId,md5OldPwd,md5Pwd,md5Repwd];
        suburl = [suburl stringByAppendingString:@"app000009"];
        NSString *url = [NSString stringWithFormat:@"%@%@",kServerAddress,suburl];
        url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[RYDownloaderManager sharedManager] requestDataByGetWithURLString:url delegate:self purpose:nil];
    }

}

- (NSString *)checkValid
{
    /**
     动态密码：4-4
     新密码：6-50
     */
    if(oldPwdField.text.length == 0)
        return [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000001E"],@"旧密码"];
    else if(oldPwdField.text.length < 6 || oldPwdField.text.length > 50)
        return [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000012E"],@"6-50位",@"旧密码"];
    else if(pwdField.text.length == 0)
        return [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000001E"],@"新密码"];
    else if(pwdField.text.length < 6 || pwdField.text.length > 50)
        return [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000012E"],@"6-50位",@"新密码"];
    else if(repwdField.text.length == 0)
        return [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000001E"],@"确认密码"];
    else if(repwdField.text.length < 6 || repwdField.text.length > 50)
        return [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000012E"],@"6-50位",@"确认密码"];
    else if(![pwdField.text isEqualToString:repwdField.text])
        return [kRegisterPromptDict objectForKey:@"F000013E"];
    else
        return nil;
}

#pragma mark - BaseViewController methods
- (void)leftItemTapped
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UIViewController methods

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNaviTitle: @"登录密码"];
    [self setLeftNaviItemWithTitle:@"返回" imageName:@"Icon_Back"];
    [self setRightNaviItemWithTitle:nil imageName:nil];
    
    CGSize scrollSize = self.myScrollView.contentSize;
    scrollSize.height = 300.f;
    self.myScrollView.contentSize = scrollSize;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.isViewLoaded && !self.view.window)
    {
        [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        self.view = nil;
        self.oldPwdField = nil;
        self.pwdField = nil;
        self.repwdField = nil;
        self.forgetPwdButton = nil;
        self.myScrollView = nil;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.oldPwdField = nil;
    self.pwdField = nil;
    self.repwdField = nil;
    self.forgetPwdButton = nil;
    self.myScrollView = nil;
}

- (void)dealloc
{
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [oldPwdField release];
    [pwdField release];
    [repwdField release];
    [forgetPwdButton release];
    [myScrollView release];
    [super dealloc];
}

#pragma mark - RYDownloaderDelegate methods
- (void)downloader:(RYDownloader*)downloader completeWithNSData:(NSData*)data
{
    NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    //去掉被包的jsonp字串
    if(str.length > 7)
        str = [str substringWithRange:NSMakeRange(6, str.length-7)];

    NSDictionary *dict = [str JSONValue];
    if([[dict objectForKey:@"result"] isEqualToString:@"1"])
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    [[RYHUDManager sharedManager] showWithMessage:[dict objectForKey:@"message"] customView:nil hideDelay:2.f];

}
- (void)downloader:(RYDownloader*)downloader didFinishWithError:(NSString*)message
{
    [[RYHUDManager sharedManager] showWithMessage:kNetWorkErrorString customView:nil hideDelay:2.f];
}

#pragma mark - notification methord

- (void)keyboardWillShow:(NSNotification *)notification
{
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect scrollFrame = self.myScrollView.frame;
    scrollFrame.size.height = self.view.frame.size.height - keyboardSize.height - 44;
    self.myScrollView.frame = scrollFrame;
}

- (void) keyboardWillHide:(NSNotification *)notification
{
    CGRect scrollFrame = self.myScrollView.frame;
    scrollFrame.size.height = self.view.frame.size.height - 44;
    self.myScrollView.frame = scrollFrame;
}
@end
